<?php
include 'config.php';

mysql_connect($db['host'], $db['user'], $db['pass']) or die('Error: ' . mysql_error());
mysql_select_db($db['name']) or die('Error: ' . mysql_error());

session_start();

ob_start();

$definedfuncs = get_defined_functions();
$definedfuncs = $definedfuncs['user'];

$adminfuncs = array();

foreach ($definedfuncs as $adminfunc) {
	if (substr(strtolower($adminfunc),0,13) == 'gc_das_admin_') {
		$adminfuncs[] = strtolower($adminfunc);
	}
}

function cleanurl ($url,$path=false) {
	$url = parse_url($url);
	
	if (array_key_exists('host', $url)) {
		$url['host'] = str_replace('www.', '', $url['host']);
		$url = ($path!=false) ? $url['host'].((array_key_exists('path', $url) != false) ? $url['path'] : '/') : $url['host'];
		return($url);
	}
	return(false);
}

function submittosite($postthis,$siteurl) {
	$ch = curl_init($siteurl);
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $postthis);
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
	curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1; U; en; rv:1.8.1) Gecko/20061208 Firefox/2.0.0 Opera 9.64');
	curl_setopt($ch, CURLOPT_HEADER, 1);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	$contents = curl_exec($ch);
}

function gc_das_admin_home() {
	$return = null;
	$return .= 'Welcome to the <i>gc-</i>DDLAutoSubmitter administration panel. <br><br>';
	$return .= 'You might want to <a href="http://global-config.com/">check for updates</a>.';
	return($return);
}

function gc_das_admin_ddl_site_queue() {
	$return = null;
	
	if (isset($_REQUEST['delsel'])) {
		$i=0;
		$selected = $_REQUEST['selectqueue'];
		foreach ($selected as $siteid) {
			
			$siteidexists = mysql_query('select id from gc_das_qsites where id="'.$siteid.'"') or die('Error: '. mysql_error());
			if (mysql_num_rows($siteidexists) == 1) {
				mysql_query('delete from gc_das_qsites where id="'.$siteid.'"') or die('Error: '. mysql_error());
				$i++;
			}
			
		}
		$return .= 'You have deleted ' . $i . ' site(s). <br><br>';
	}
	
	if (isset($_REQUEST['addsel'])) {
		$i=0;
		$selected = $_REQUEST['selectqueue'];
		foreach ($selected as $siteid) {
			
			$siteidexists = mysql_query('select * from gc_das_qsites where id="'.$siteid.'"') or die('Error: '. mysql_error());
			if (mysql_num_rows($siteidexists) == 1) {
				while($siteinfo = mysql_fetch_assoc($siteidexists)) {
					$existsinsites = mysql_query('select id from gc_das_sites where siteurl LIKE "%'.$siteinfo['siteurl'].'%"') or die ('Error: '.mysql_error());
					if (mysql_num_rows($existsinsites) == 0) {
						mysql_query('insert into gc_das_sites (sitename, siteurl, submiturl, adminemail) VALUES ("'.$siteinfo['sitename'].'","'.$siteinfo['siteurl'].'","'.$siteinfo['submiturl'].'","'.$siteinfo['adminemail'].'")') or die('Error: ' . mysql_error());
						mysql_query('delete from gc_das_qsites where id="'.$siteinfo['id'].'"') or die ('Error: '. mysql_query());
						$i++;
					}
				}
			}
			
		}
		$return .= 'You have added ' . $i . ' site(s). <br><br>';
	}
	
	$getsitequeue = mysql_query('select * from gc_das_qsites order by id desc') or die('Error: ' . mysql_error());
	if (mysql_num_rows($getsitequeue) != 0) {
		$return .= '<form action="" method="post">';
		while ($gsq = mysql_fetch_assoc($getsitequeue)) {
			$return .= '<input type="checkbox" name="selectqueue[]" value="'.$gsq['id'].'" /> - <a href="http://anonym.to/?http://'.$gsq['siteurl'].'">'.$gsq['sitename'].'</a> - <a href="http://anonym.to/?http://'.$gsq['submiturl'].'">http://'.$gsq['submiturl'].'</a><br>';
		}
		$return .= '<input type="submit" name="addsel" value="Add Selected" /> | <input type="submit" name="delsel" value="Delete Selected" /></form>';
	}
	else
	{
		$return .= 'There are no sites in the queue.';
	}
	
	
	return($return);
}

function gc_das_admin_delete_sites() {
	$return = null;
	
	if (isset($_REQUEST['delsel'])) {
		$i=0;
		$selected = $_REQUEST['selectqueue'];
		foreach ($selected as $siteid) {
			
			$siteidexists = mysql_query('select id from gc_das_sites where id="'.$siteid.'"') or die('Error: '. mysql_error());
			if (mysql_num_rows($siteidexists) == 1) {
				mysql_query('delete from gc_das_sites where id="'.$siteid.'"') or die('Error: '. mysql_error());
				$i++;
			}
			
		}
		$return .= 'You have deleted ' . $i . ' site(s). <br><br>';
	}
	
	
	$getsitequeue = mysql_query('select * from gc_das_sites order by id desc') or die('Error: ' . mysql_error());
	if (mysql_num_rows($getsitequeue) != 0) {
		$return .= '<form action="" method="post">';
		while ($gsq = mysql_fetch_assoc($getsitequeue)) {
			$return .= '<input type="checkbox" name="selectqueue[]" value="'.$gsq['id'].'" /> - <a href="http://anonym.to/?http://'.$gsq['siteurl'].'">'.$gsq['sitename'].'</a> - <a href="http://anonym.to/?http://'.$gsq['submiturl'].'">http://'.$gsq['submiturl'].'</a><br>';
		}
		$return .= '<input type="submit" name="delsel" value="Delete Selected" /></form>';
	}
	else
	{
		$return .= 'There are no sites in the database.';
	}
	
	
	return($return);
}

if (isset($_REQUEST['submit'])) {
	
	$subssname = $_REQUEST['sname'];
	$subssurl = $_REQUEST['surl'];
	$subssemail = $_REQUEST['email'];
	
	$substitle = $_REQUEST['title'];
	$subsurl = $_REQUEST['url'];
	$substype = $_REQUEST['type'];
	
	$subspostme = null;
	
	if (!empty($substitle[0]) && !empty($subsurl[0]) && !empty($substype[0]) && !empty($subssname) && !empty($subssurl) && !empty($subssemail)) {
		for ($i=0;$i<=9;$i++) {
			$subspostme .= 'title[]='.urlencode(stripslashes($substitle[$i])).'&url[]='.urlencode(stripslashes($subsurl[$i])).'&type[]='.urlencode(stripslashes($substype[$i])) . '&';
		}
		$subspostme .= 'sname='.urlencode(stripslashes($subssname)).'&surl='.urlencode(stripslashes($subssurl)).'&email='.urlencode(stripslashes($subssemail));
	}
	
	
}

if (isset($_REQUEST['admin_gc_das_sub'])) {
	if ($_REQUEST['admin_gc_das_login'] == $adminpass) {
		$_SESSION['gc_das_al'] = 'yea';
	}
}
?>